1696E - Placing Jinas - CodeForces Solution


combinatorics math *2000

Please click on ads to support us..

Python Code:

MOD=10**9+7
ans=0
fact=[1]
for i in range(1,4*10**5+5):
    fact.append(fact[-1]*i%MOD)
def c(m,n):
    return fact[m]*pow(fact[m-n]*fact[n],-1,MOD)
n=int(input())
for i,a in enumerate(map(int,input().split())):
    if a>0:
        ans+=c(a+i,i+1)%MOD
        ans%=MOD
print(ans)

C++ Code:

#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define sz(x) ((int) x.size())
#define all(x) x.begin(), x.end()
typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vii;
typedef vector<ll> vl;

const int M = 1e9 + 7;
const int maxn = 5e5 + 1;

ll fexp(ll a, ll b) {
    if (b == 0) return 1;
    ll f = fexp(a, b / 2);
    f = (f * f) % M;
    if (b % 2) f = (f * a) % M;
    return f;
}

ll inv(ll n) {
    return fexp(n, M - 2);
}

ll fat[maxn];

ll chs(ll n, ll k) {
    return (fat[n] * inv((fat[n - k] * fat[k]) % M)) % M;
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

    fat[0] = 1;
    for (int i = 1; i < maxn; ++i) {
        fat[i] = (fat[i - 1] * i) % M;
    }

    int n; cin >> n; ++n;
    vi a(n);
    for (int &x : a) cin >> x;

    ll ans = 0;

    for (int i = 0; i < n; ++i) {
        if (a[i] == 0) break;
        ans = (ans + chs(i + a[i], a[i] - 1)) % M;
    }

    cout << ans << '\n';

    return 0;
}


Comments

Submit
0 Comments
More Questions

36. Valid Sudoku
557. Reverse Words in a String III
566. Reshape the Matrix
167. Two Sum II - Input array is sorted
387. First Unique Character in a String
383. Ransom Note
242. Valid Anagram
141. Linked List Cycle
21. Merge Two Sorted Lists
203. Remove Linked List Elements
733. Flood Fill
206. Reverse Linked List
83. Remove Duplicates from Sorted List
116. Populating Next Right Pointers in Each Node
145. Binary Tree Postorder Traversal
94. Binary Tree Inorder Traversal
101. Symmetric Tree
77. Combinations
46. Permutations
226. Invert Binary Tree
112. Path Sum
1556A - A Variety of Operations
136. Single Number
169. Majority Element
119. Pascal's Triangle II
409. Longest Palindrome
1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections